package com.shrw.duke.serialport.utils;


import android.util.Log;

/**
 * Created by rw-duke on 2018/4/2.
 */

public class PortDataTrimUtils {
    private static PortDataTrimUtils instance;
    private byte[] mCache = new byte[3072];
    private int mCacheLength = 0;

    public static PortDataTrimUtils getInstance() {
        if (instance == null) {
            synchronized (PortDataTrimUtils.class) {
                if (instance == null) {
                    instance = new PortDataTrimUtils();
                }
            }
        }
        return instance;
    }


    public boolean add(byte[] src, int length) {
        try {
            if (src != null && length > 0) {
//            byte[] cacheCopy = new byte[mCacheLength+length];
//            System.arraycopy(mCache, 0, cacheCopy, 0, mCacheLength);
//            Log.e("PortReadRunnable3:", mCacheLength + "");
                System.arraycopy(src, 0, mCache, mCacheLength, length);
//            mCache = cacheCopy;
                mCacheLength = mCacheLength + length;
                return true;
            }
        }catch (ArrayIndexOutOfBoundsException e){
            remove(mCache,mCacheLength);
            return false;
        }
        return false;
    }

    public byte[] get() {
        int headIndex = getHeadIndex(mCache);//头的下标
        if (headIndex != -1) {
//            String s;
            int length = ((mCache[headIndex + 4] & 0xff) * 256) + (mCache[headIndex + 5] & 0xff);//完整数据的长度
//            Log.e("FullscreenActivity length:", Integer.toHexString(0xFF & mCache[headIndex + 4]) + Integer.toHexString(0xFF & mCache[headIndex + 5]));
            if ((mCacheLength - headIndex) >= length) {
                byte[] des = new byte[length];//一包完整数据
                System.arraycopy(mCache, headIndex, des, 0, length);
                remove(mCache, headIndex + length);
                return des;
            }
            return null;
        }
        return null;
    }

    private int getHeadIndex(byte[] src) {
        byte b1;
        byte b2;
        byte b3;
        byte b4;
        for (int i = 0; i < src.length - 4; i++) {
            b1 = src[i];
            b2 = src[i + 1];
            b3 = src[i + 2];
            b4 = src[i + 3];
            if (b1 == (byte) 2 && b2 == (byte) 3 && b3 == (byte) 4 && b4 == (byte) 5) {
                return i;
            }
        }
        return -1;
    }

    private void remove(byte[] original, int toIndex) {

        if (toIndex > mCacheLength) {
            throw new IndexOutOfBoundsException("toIndex > mCacheLength");
        }
        int numMoved = mCacheLength - toIndex;
//        byte[] copy = new byte[numMoved];
        System.arraycopy(original, toIndex, original, 0, numMoved);
        mCacheLength = numMoved;
//        return copy;
//        // clear to let GC do its work
//        int newSize = mCacheLength - (toIndex-fromIndex);
//
//        for (int i = newSize; i < mCacheLength; i++) {
//            original[i] = null;
//        }
//        mCacheLength = newSize;
//
//
//
//        if (toIndex < fromIndex) {
//            throw new IndexOutOfBoundsException("toIndex < fromIndex");
//        }
//        int newLength = fromIndex + original.length - toIndex - 1;
//        byte[] copy = new byte[newLength];
//        if (fromIndex ==0){
//            System.arraycopy(original, 0, copy, 0,fromIndex);
//        }
//        System.arraycopy(original, toIndex, copy, fromIndex,
//                Math.min(original.length - toIndex, newLength));
//        mCacheLength = toIndex - fromIndex;
//        return copy;
    }
}
